package cn.jdy.plugins.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.Organ;
import cn.jdy.authority.entity.User;
import cn.jdy.authority.entity.UserRole;
import cn.jdy.authority.service.OrganService;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.HttpUtil;
import cn.jdy.framework.util.JsonUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.entity.News;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Line;
import cn.jdy.plugins.entity.Live;
import cn.jdy.plugins.entity.PptImg;
import cn.jdy.plugins.entity.RelatedVideo;
import cn.jdy.plugins.entity.WatchUser;
import cn.jdy.plugins.entity.WxConfig;
import cn.jdy.plugins.service.AdvertisingService;
import cn.jdy.plugins.service.ApplyCashService;
import cn.jdy.plugins.service.ChatService;
import cn.jdy.plugins.service.LabelService;
import cn.jdy.plugins.service.LineService;
import cn.jdy.plugins.service.LiveService;
import cn.jdy.plugins.service.PptImgService;
import cn.jdy.plugins.service.RelatedVideoService;
import cn.jdy.plugins.service.WatchUserService;
import cn.jdy.plugins.service.WxConfigService;
import cn.jdy.plugins.wap.util.ChatUtil;
import cn.jdy.plugins.wap.util.EmojiFilter;

/**
 * @title 获取用户信息,调用微信图片接口
 * @author ningqian
 * @since 2017-3-23,2017-4-5
 */

@Controller
@RequestMapping("/jk-bin")
public class JkLiveCtrl extends BaseCtrl {
	@Autowired
	private LiveService liveService;


	
	

	@SuppressWarnings("static-access")
	@RequestMapping("toError")
	public ModelAndView toError() {
		ModelAndView mv = new ModelAndView(super.t + "/error.jsp");
		return mv;
	}
	/**
	 * 异步查询
	 * 
	 * @param  appid  机构id，iDisplayStart 开始记录，iDisplayLength 结束记录
	 * 
	 * @return   
	 * {
		"msg":  "成功"，  //调用信息
		"recordsTotal":        ""， //记录总数
		 "dataList":
		            [
		            {
		            "id" :           ""，  //ID
		            "mainurl":       ""， //线路
		            "createTime":    ""， //创建时间
		            "status":        ""， //播放状态 0 片花 1 直播 2回播
		            "name":          ""， //直播名称
		            "zburl":          ""， //直播地址
		                "liveLevel":    1， //级别
		            }
		            ]
		}
	 */
	@RequestMapping("/organ/list")
	@ResponseBody
	public Map<String, Object> queryByPage(String organid ,int iDisplayStart ,int iDisplayLength,String liveLevel  ) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
				jsonMap.put("msg", "成功");
				jsonMap.put("dataList", liveService.queryOrganMap(organid,iDisplayStart,iDisplayLength,liveLevel));
				jsonMap.put("recordsTotal", liveService.countOrganMap(organid,iDisplayStart,iDisplayLength,liveLevel));
			
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
			jsonMap.put("msg", "查询异常");
			jsonMap.put("dataList", "");
			jsonMap.put("iTotalRecords", "");
		}
		return jsonMap;
	}
	
	/**
	 * 异步查询
	 * 
	 * @param  appid  公众号id，
	 * 
	 * @return   
	 * {
		"msg":  "成功"，  //调用信息
		 "dataList":
		            [
		            {
		            "brlist":[{"zs":0,"yt":"本日观看"}] //本日
		             ,"bylist":[{"zs":0,"yt":"本月观看"}] //本月
		             ，"ylist":[2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,1,89,17] //近30天统计的值
		             ， "ynamelist":["8-17","8-16","8-15","8-14","8-13","8-12","8-11","8-10","8-09","8-08","8-07","8-06","8-05","8-04","8-03","8-02","8-01","8-31","8-30","8-29","8-28","8-27","8-26","8-25","8-24","8-23","8-22","8-21","8-20","8-19"],
		           	 ，"qtnamelist":["8-24","8-23","8-22","8-21","8-20","8-19","8-18"],  //近7天时间
		             ，"qtlist":[0,0,0,1,89,17,12]  //近7天统计值
		            }
		            ]
		}
	 */
	@RequestMapping("/organ/liveCountliveQuery")
	@ResponseBody
	public Map<String, Object> liveCountliveQuery(String organid  ) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
				jsonMap.put("msg", "成功");
				jsonMap.put("dataList", liveService.liveCountliveQuery(organid));
			
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
			jsonMap.put("msg", "查询异常");
			jsonMap.put("dataList", "");
			jsonMap.put("iTotalRecords", "");
		}
		return jsonMap;
	}
	
	/**
	 * 异步查询
	 * 
	 * @param  appid  公众号id
	 * 
	 * @return   
	 * {
		"msg":  "成功"，  //调用信息
		"recordsTotal":        ""， //记录总数
		 "dataList":
		            [
		            {
		            "id" :           ""，  //ID
		            "mainurl":       ""， //线路
		            "createTime":    ""， //创建时间
		            "status":        ""， //播放状态 0 片花 1 直播 2回播
		            "name":          ""， //直播名称
		            "zburl":          ""， //直播地址
		            "liveLevel":    1， //级别
		            }
		            ]
		}
	 */
	@RequestMapping("/live/edit")
	@ResponseBody
	public Map<String, Object> queryByPage(String liveId) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		try {
				//jsonMap.put("msg", "1");
				//jsonMap.put("msg", "成功");
			jsonMap= liveService.queryLiveMap(Long.valueOf(liveId));
			 
			
		} catch (Exception e) {
		
			BaseLog.e(this.getClass(), "查询异常", e);
		//	jsonMap.put("msg", "0");
		//	jsonMap.put("msg", "查询异常");
		//	jsonMap.put("dataList", "");
		}
		return jsonMap;
	}
	
	



}
